﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Flower.SqlServer.Directory.DbGenerator.Generators;

namespace Flower.SqlServer.Directory.DbGenerator
{
    internal class Program
    {
        public const string ScriptsNamespace = "Flower.SqlServer.Directory.DbGenerator.Scripts";

        private static readonly IGenerator[] Version0_1 = new IGenerator[] 
        {
            new Tables(), 
            new Script(1, "Types"),
            new Script(2, "ProceduresAndFunctions"),
            new SaveProcedures(),
            new GetProcedure(),
            new GetChildrenProcedure(),
            new GetAncestorProcedure(),
            new GetDataProcedure(),
            new RemoveProcedure(),
            new Init(),
        };

        private static readonly Dictionary<string, IEnumerable<IGenerator>> Generators = new Dictionary<string, IEnumerable<IGenerator>> 
        {
            { "v0.1", Version0_1 }
        };

        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: flwsqlgen <generators> <options>");
                return;
            }

           IEnumerable<IGenerator> generators;

            if (!Generators.TryGetValue(args[0], out generators))
            {
                Console.WriteLine("Unknown script version {0}.", args[0]);
                return;
            }

            var settings = new Settings();
            TextWriter writer = Console.Out;
            bool disposeWriter = false;

            int i = 1;

            try
            {
                while (i < args.Length)
                {
                    switch (args[i])
                    {
                        case "/o":
                            ++i;
                            if (i < args.Length)
                            {
                                writer = new StreamWriter(args[i]);
                                disposeWriter = true;
                            }
                            break;
                        case "/adm":
                            ++i;
                            if (i < args.Length)
                            {
                                settings.DefaultAdmin = args[i];
                            }
                            break;
                    }

                    ++i;
                }

                foreach (IGenerator generator in generators.OrderBy(gen => gen.Order))
                {
                    generator.Generate(writer, settings);

                    writer.WriteLine();
                }
            }
            finally
            {
                if (disposeWriter)
                {
                    writer.Dispose();
                }
            }
        }
    }
}
